package org.jeecg.modules.business.service.impl;

import org.jeecg.modules.business.entity.ContractInformation;
import org.jeecg.modules.business.entity.DeliveryDetail;
import org.jeecg.modules.business.entity.WorkDetails;
import org.jeecg.modules.business.entity.PaymentDetails;
import org.jeecg.modules.business.mapper.DeliveryDetailMapper;
import org.jeecg.modules.business.mapper.WorkDetailsMapper;
import org.jeecg.modules.business.mapper.PaymentDetailsMapper;
import org.jeecg.modules.business.mapper.ContractInformationMapper;
import org.jeecg.modules.business.service.IContractInformationService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 合同信息表
 * @Author: jeecg-boot
 * @Date:   2020-12-03
 * @Version: V1.0
 */
@Service
public class ContractInformationServiceImpl extends ServiceImpl<ContractInformationMapper, ContractInformation> implements IContractInformationService {

	@Autowired
	private ContractInformationMapper contractInformationMapper;
	@Autowired
	private DeliveryDetailMapper deliveryDetailMapper;
	@Autowired
	private WorkDetailsMapper workDetailsMapper;
	@Autowired
	private PaymentDetailsMapper paymentDetailsMapper;

	
	@Override
	@Transactional
	public void saveMain(ContractInformation contractInformation, List<DeliveryDetail> deliveryDetailList,List<WorkDetails> workDetailsList,List<PaymentDetails> paymentDetailsList) {
		contractInformationMapper.insert(contractInformation);
		if(deliveryDetailList!=null && deliveryDetailList.size()>0) {
			for(DeliveryDetail entity:deliveryDetailList) {
				//外键设置
				entity.setDeliveryId(contractInformation.getId());
				deliveryDetailMapper.insert(entity);
			}
		}
		if(workDetailsList!=null && workDetailsList.size()>0) {
			for(WorkDetails entity:workDetailsList) {
				//外键设置
				entity.setWorkId(contractInformation.getId());
				workDetailsMapper.insert(entity);
			}
		}
		if(paymentDetailsList!=null && paymentDetailsList.size()>0) {
			for(PaymentDetails entity:paymentDetailsList) {
				//外键设置
				entity.setPaymentId(contractInformation.getId());
				paymentDetailsMapper.insert(entity);
			}
		}

	}

	@Override
	@Transactional
	public void updateMain(ContractInformation contractInformation,List<DeliveryDetail> deliveryDetailList,List<WorkDetails> workDetailsList,List<PaymentDetails> paymentDetailsList) {
		contractInformationMapper.updateById(contractInformation);
		
		//1.先删除子表数据
		deliveryDetailMapper.deleteByMainId(contractInformation.getId());
		workDetailsMapper.deleteByMainId(contractInformation.getId());
		paymentDetailsMapper.deleteByMainId(contractInformation.getId());

		
		//2.子表数据重新插入
		if(deliveryDetailList!=null && deliveryDetailList.size()>0) {
			for(DeliveryDetail entity:deliveryDetailList) {
				//外键设置
				entity.setDeliveryId(contractInformation.getId());
				deliveryDetailMapper.insert(entity);
			}
		}
		if(workDetailsList!=null && workDetailsList.size()>0) {
			for(WorkDetails entity:workDetailsList) {
				//外键设置
				entity.setWorkId(contractInformation.getId());
				workDetailsMapper.insert(entity);
			}
		}
		if(paymentDetailsList!=null && paymentDetailsList.size()>0) {
			for(PaymentDetails entity:paymentDetailsList) {
				//外键设置
				entity.setPaymentId(contractInformation.getId());
				paymentDetailsMapper.insert(entity);
			}
		}

	}

	@Override
	@Transactional
	public void delMain(String id) {
		deliveryDetailMapper.deleteByMainId(id);
		workDetailsMapper.deleteByMainId(id);
		paymentDetailsMapper.deleteByMainId(id);
		contractInformationMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			deliveryDetailMapper.deleteByMainId(id.toString());
			workDetailsMapper.deleteByMainId(id.toString());
			paymentDetailsMapper.deleteByMainId(id.toString());
			contractInformationMapper.deleteById(id);
		}
	}
	
}
